
<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <!-- 1.头部 -->
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="doShowDrawer('add')">
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 2.表格 -->
        <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="stem"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="作者">
      </el-table-column>
      <el-table-column
        prop="likeCount"
        label="点赞">
      </el-table-column>
      <el-table-column
        prop="views"
        label="浏览数">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="更新时间">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{row}">
            <div class="actions">
            <i class="el-icon-view" @click="doShowDrawer('preview',row.id)"></i>
            <i class="el-icon-edit-outline" @click="doShowDrawer('edit',row.id)"></i>
            <i class="el-icon-delete" @click="doDelete(row.id)"></i>
          </div>
          </template>
      </el-table-column>
    </el-table>
      <!-- 3.分页组件:
      3:分页组件
page-size:每页数量
total:总数量Delete
自动计算页码数量=total/page-size
page-count:设置页码数量
一旦有tota1,这个属性就会无效
pager-count：页码按钮数量5-21的奇数
current-page:当前页
page-sizes:每页条数数组
       -->
     <el-pagination
      :current-page="current"
      :page-sizes="[5, 50, 100, 200]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :pager-count="9"
      :background="true"
      @size-change="doSizeChange"
      @current-change="doCurrentChange"
      >
    </el-pagination>

    </el-card>
    <!-- 抽屉组件 -->
  <el-drawer
  title="我是标题"
  :visible.sync="showDrawer"
  direction="rtl"
  size='50%'
  :before-close="handleClose">
  <!-- //from表单 -->
  <div v-if='drawerType==="preview"'>
    <h3>{{formData.stem}}</h3>
    <div v-html="formData.content"></div>
  </div>
  <el-form v-else label-width="80px" :model='formData' :rules="formDataRules" ref='formRef'>
    <el-form-item label="标题" prop="stem">
      <el-input placeholder="请输入标题" v-model="formData.stem" ></el-input>
    </el-form-item>
    <el-form-item label="内容" prop='content'>
      <!-- 富文本框 -->
      <quillEditor  v-model="formData.content"></quillEditor>
    </el-form-item>
  <el-form-item>
    <el-button type="primary" @click='doSubmit'>提交</el-button>
    <el-button @click="doCancel">取消</el-button>
  </el-form-item>

  </el-form>
  </el-drawer>
  </div>
</template>

<script>
import { getArticleList, addArticle, getArticleInof, etitArticle, delitArticle } from '@/api/acticle'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'InterviewPcIndex',
  components: {
    quillEditor
  },

  data () {
    return {
      // 当前页
      current: 1,
      // 每页数量,
      pageSize: 20,
      // 面经列表
      list: [],
      // 总数量
      total: 0,
      // 抽屉弹窗显示/隐藏
      showDrawer: false,
      // 抽屉类型add/preview/edit
      drawerType: 'add',
      formData: {
        // 表单数据
        stem: '',
        content: ''
      },
      // 表单校验
      formDataRules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },

  mounted () {
    this.getLIst()
  },

  methods: {

    // 1 获取列表
    async getLIst () {
      const { data } = await getArticleList(
        {
          // 绑定数据
          current: this.current,
          pageSize: this.pageSize
        }
      )
      console.log(data.data.rows)
      // 绑定数据:列表+总数量
      this.list = data.data.rows
      this.total = data.data.total
    },
    // 2每页条数的变化
    doSizeChange (size) {
      console.log(size)
      // 修改每页数量
      this.pageSize = size
      this.getLIst()
    },
    // 3页码变化
    doCurrentChange (page) {
      console.log(page)
      // 修改页码
      this.current = page
      // 刷新数据
      this.getLIst()
    },
    // 4点击,新增编辑预览
    async doShowDrawer (type, id) {
      // 存储抽屉类型add/preview/edit
      this.drawerType = type
      // (2)显示抽屉弹窗
      this.showDrawer = true
      // 获取面经详情
      const { data } = await getArticleInof(id)
      console.log(data)
      // 绑定数据
      this.formData = data.data
    },
    // 5.抽屉隐藏事件
    handleClose () {
      this.showDrawer = false
      // 清空 表单数据
      this.formData = {
        stem: '',
        content: ''
      }
    },
    // 6重置表单 + 关闭抽屉
    doCancel () {
      this.$refs.formRef.resetFields()
      this.handleClose()
    },
    // 7. 判断校验是否通过
    doSubmit () {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          // (1)发送ajax请求
          // 判断编辑和新增
          if (this.formData.id) {
            await etitArticle(this.formData)
            // (2)提示框
            this.$message.success('编辑成功')
          } else {
            await addArticle(this.formData)
            // (2)提示框
            this.$message.success('添加成功')
          }

          // 刷新列表
          this.getLIst()
          // 清空表单
          this.handleClose()
        }
      })
    },
    // 8删除类表
    async doDelete (id) {
      await delitArticle(id)
      this.$message.success('删除成功')
      /* 删除优化公：
      如果当前删除的是本项的最后一条数据。
      则应该页吗-1 */
      if (this.total % this.pagesize === 1) {
        this.current--
      }
      // 刷新列表
      this.getLIst()
    }
  },
  computed: {
    drawerTitle () {
      if (this.drawerType === 'add') {
        return '新增文章'
      } else if (this.drawerType === 'edit') {
        return '编辑文章'
      } else {
        return '预览文章'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
